package org.subside.restTemplate;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        //Get请求
        //方法一：getForEntity(String var1, Class<T> var2)，没有参数
        String url = "https://restapi.amap.com/v3/ip?key=075b6eddd825148a674dfa8a8558ac62";
        String trans = restTemplate.getForObject(url,String.class);
        System.out.println(trans);
        //Post请求
        /*
        1.postForObject(String url,Object request,Class responseType,Object... uriVariables)
        2.postForObject(String url,Object request,Class responseType,Map uriVariables)
        3.postForObject(URI url,Object request，Class responseType)
         */

        //方法一：getForEntity(String var1, Class<T> var2, Object... var3)，url中用占位符，传入参数
        //该方法提供了三个参数，其中var1为请求的地址(即url)，var2为请求响应body的包装类型，var3为url中的参数绑定
        url = "https://restapi.amap.com/v3/ip?key={?}";
        trans = restTemplate.getForObject(url,String.class,"075b6eddd825148a674dfa8a8558ac62");
        System.out.println(trans);

        //方法二：getForEntity(String var1, Class<T> var2, Map<String, ?> var3)，map传参
        url = "https://restapi.amap.com/v3/ip?key={key}";
        Map<String,Object> map = new HashMap<>();
        map.put("key","075b6eddd825148a674dfa8a8558ac62");
        trans = restTemplate.getForObject(url,String.class,map);
        System.out.println(trans);

        //方法三：getForEntity(URI var1, Class<T> var2)，uri传参
        URI uri = URI.create("https://restapi.amap.com/v3/ip?key=075b6eddd825148a674dfa8a8558ac62");
        trans = restTemplate.getForObject(uri,String.class);
        System.out.println(trans);
    }
}
